home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / cuj0696.zip / DWYER.ZIP / POKER.TST / PKRCHISQ.C < prev    next >
C/C++ Source or Header  |  1996-01-07  |  4KB  |  130 lines

  1. /* ============ */
  2. /* pkrchisq.c    */
  3. /* ============ */
  4. #include <defcodes.h>
  5. #include <pokrdefs.h>
  6.  
  7. /* ------------------- */
  8. /* FUNCTION PROTOTYPES */
  9. /* ------------------- */
  10. # undef F
  11. # if defined(__STDC__) || defined(__PROTO__)
  12. #    define  F( P )  P
  13. # else
  14. #    define  F( P )  ()
  15. # endif
  16.  
  17. /* INDENT OFF */
  18. extern    void    CalcPokerChiSq F((struct PokerDataStru *));
  19. static    int    DealNewHand F((struct PokerDataStru *));
  20.  
  21. # undef F
  22. /* INDENT ON */
  23.  
  24. long    NDiffCtrs[MAX_CARDS];
  25.  
  26. #define    DELETED    -1
  27. /* ==================================================================== */
  28. /* CalcPokerChiSq - Calculates Chi-Square Statistic for Poker Test    */
  29. /* ==================================================================== */
  30. void
  31. CalcPokerChiSq(POKER_DATA_STRU * PokerData)
  32. {
  33.     /* ---------------------------------------------------------------- */
  34.     /* Clear NDiff counters                         */
  35.     /* Calculate Poker-Probabilities for this CardsPerHand & DataSize    */
  36.     /*                                    */
  37.     /* for j = 1 to NumHands                        */
  38.     /*      1. Deal a hand, get NDiff                    */
  39.     /*      2. Increment corresponding NDiff counter            */
  40.     /*                                    */
  41.     /* Calculate Chi-Square Statistic, Store at PokerData->PokerChiSq    */
  42.     /*                                    */
  43.     /* Return                                */
  44.     /* ---------------------------------------------------------------- */
  45.  
  46.     int     j;
  47.     long    k;
  48.  
  49.     memset(NDiffCtrs, 0, sizeof(NDiffCtrs));
  50.  
  51.     for (k = 1; k <= PokerData->NumHands; ++k)
  52.     {
  53.     int    NumDiff;
  54.     NumDiff = DealNewHand(PokerData);
  55.     ++NDiffCtrs[NumDiff-1];
  56.     }
  57.     /* --------------------------- */
  58.     /* Lump Categories as Required */
  59.     /* --------------------------- */
  60.     for (j = 0; j < PokerData->NotEnough; ++j)
  61.     {
  62.     NDiffCtrs[j+1] += NDiffCtrs[j];
  63.     }
  64.  
  65.     /* ------------------------------ */
  66.     /* Calculate Chi-Square Statistic */
  67.     /* ------------------------------ */
  68.     PokerData->PokerChiSq = 0;
  69.  
  70.     for (j = PokerData->NotEnough; j < PokerData->CardsPerHand; ++j)
  71.     {
  72.     if (NDiffCtrs[j])
  73.     {
  74.         PokerData->PokerChiSq +=
  75.         SQR((double)NDiffCtrs[j]) / PokerData->CellExpect[j];
  76.     }
  77.     P(printf("\tPokerChiSq = %.15e\n", PokerData->PokerChiSq));
  78.     }
  79.  
  80.     PokerData->PokerChiSq -= PokerData->NumHands;
  81.     PokerData->CallStatusOK = TRUE;
  82. }
  83. /* ==================================================================== */
  84. /* DealNewHand - Deals a Hand of Size CardsPerHand and Returns NumDiff    */
  85. /* ==================================================================== */
  86. static    int
  87. DealNewHand(POKER_DATA_STRU  *PokerData)
  88. {
  89.     int     j, k, NumDiff;
  90.     int     ThisHand[MAX_CARDS];
  91.  
  92.     /* ------------------------------------------------ */
  93.     /* Deal This Hand of PokerData->CardsPerHand cards.    */
  94.     /* Count Number of Different Cards in the Hand.    */
  95.     /* ------------------------------------------------ */
  96.     for (j = 0; j < PokerData->CardsPerHand; ++j)
  97.     {
  98.     ThisHand[j] = PokerData->RandFun() % PokerData->DataSize;
  99.     }
  100.  
  101.     /* -------------------------------------- */
  102.     /* Accumulating Number Variates Generated */
  103.     /* -------------------------------------- */
  104.     PokerData->TotNumGen += PokerData->CardsPerHand;
  105.  
  106.     NumDiff = PokerData->CardsPerHand;
  107.  
  108.     /* ----------------------------- */
  109.     /* Determine Number Different by */
  110.     /* Rules of Knuth, Vol. 2. p. 62 */
  111.     /* ----------------------------- */
  112.     for (j = 0; j < PokerData->CardsPerHand-1; ++j)
  113.     {
  114.     if (ThisHand[j] == DELETED)
  115.     {
  116.         continue;
  117.     }
  118.     for(k = j + 1; k < PokerData->CardsPerHand; ++k)
  119.     {
  120.         if (ThisHand[k] == ThisHand[j])
  121.         {
  122.         ThisHand[k] = DELETED;
  123.         --NumDiff;
  124.         }
  125.     }
  126.     }
  127.  
  128.     return (NumDiff);
  129. }
  130.